const app = getApp()
Page({
  data: {
    circleList: [], //圆点数组
    awardList: [], //奖品数组
    colorCircleFirst: '#FFDF2F', //圆点颜色1
    colorCircleSecond: '#FE4D32', //圆点颜色2
    colorAwardDefault: '#fe86dd', //奖品默认颜色#F5F0FC
    colorAwardSelect: 'black', //奖品选中颜色
    indexSelect: 0, //被选中的奖品index
    isRunning: false, //是否正在抽奖
    imageAward: [], //奖品图片数组
    deskname:'',//商家名称
    textCard:'',//券
    degree: 0, //抽奖次数
    sale: [], //打折劵信息
    duration: 2000,
    showPopup: false,
    $zanui: {
      toptips: {
        show: false
      }
    },
  },

  /**
   * 获得奖项
   */
  getLotter() {
    let _this = this
    // 请求奖项
    app.post(app.url.lottery.voucher).then(re => {
      re.data.forEach(item => {
        let text = ''
        if (item.discounted == 0 || item.discounted == 100) {
          text = item.name
        } else {
          text = item.name + item.discounted + '元'
        }
        let info = {
          id: item.id,
          // deskName: item.deskName,
          deskName: null,
          text: text,
          name: item.name,
          discounted: item.discounted
        }
        _this.data.imageAward.push(text)
        console.log(_this.data.imageAward)
        _this.data.sale.push(info)
      })
      _this.getLuckSum()
      _this.setStyle()
    })
  },
//奖品弹出层
// togglePopup(){
// this.setData({
//   showPopup:true
// })
// },
  togglePopupO() {
    this.setData({
      showPopup: false
    });
  },
  /**
   * 获得抽奖次数
   */
  getLuckSum() {
    let _this = this
    let userId = {
      userId: app.globalDataO.openid.objectSid
    }
    // 请求抽奖次数
    app.post(app.url.lottery.degree, userId).then(re => {
      _this.setData({
        degree: re.data.luckCount
      })
    })
  },
  /**
   * 设置展示样式
   */
  setStyle() {
    var _this = this;
    //圆点设置
    var leftCircle = 7.5;
    var topCircle = 7.5;
    var circleList = [];
    for (var i = 0; i < 24; i++) {
      if (i == 0) {
        topCircle = 15;
        leftCircle = 15;
      } else if (i < 6) {
        topCircle = 7.5;
        leftCircle = leftCircle + 102.5;
      } else if (i == 6) {
        topCircle = 15
        leftCircle = 620;
      } else if (i < 12) {
        topCircle = topCircle + 94;
        leftCircle = 620;
      } else if (i == 12) {
        topCircle = 565;
        leftCircle = 620;
      } else if (i < 18) {
        topCircle = 570;
        leftCircle = leftCircle - 102.5;
      } else if (i == 18) {
        topCircle = 565;
        leftCircle = 15;
      } else if (i < 24) {
        topCircle = topCircle - 94;
        leftCircle = 7.5;
      } else {
        return
      }
      circleList.push({
        topCircle: topCircle,
        leftCircle: leftCircle
      });
    }
    this.setData({
      circleList: circleList
    })
    //圆点闪烁
    setInterval(function() {
      if (_this.data.colorCircleFirst == '#FFDF2F') {
        _this.setData({
          colorCircleFirst: '#FE4D32',
          colorCircleSecond: '#FFDF2F',
        })
      } else {
        _this.setData({
          colorCircleFirst: '#FFDF2F',
          colorCircleSecond: '#FE4D32',
        })
      }
    }, 500)
    //奖品item设置
    var awardList = [];
    // 间距,怎么顺眼怎么设置吧.
    var topAward = 25;
    var leftAward = 25;
    for (var j = 0; j < 8; j++) {
      if (j == 0) {
        topAward = 0;
        leftAward = 0;
      } else if (j < 3) {
        topAward = topAward;
        //166.6666是宽.15是间距.下同
        leftAward = leftAward + 186.6666 + 15;
      } else if (j < 5) {
        leftAward = leftAward;
        //150是高,15是间距,下同
        topAward = topAward + 170 + 15;
      } else if (j < 7) {
        leftAward = leftAward - 186.6666 - 15;
        topAward = topAward;
      } else if (j < 8) {
        leftAward = leftAward;
        topAward = topAward - 170 - 15;
      }
      var imageAward = this.data.imageAward[j];
      awardList.push({
        topAward: topAward,
        leftAward: leftAward,
        imageAward: imageAward
      });
    }
    console.log(awardList)
    this.setData({
      awardList: awardList
    })
  },

  onLoad: function() {
    this.getLotter()
  },
//奖品发给后台
  sendvoucher(id){
    let userInfo = {
      userId: app.globalDataO.openid.objectSid,
      id:id
    }
    app.post(app.url.lottery.award, userInfo).then(res=>{
      console.log(res)
    })
  },
  /**
   * 请求后台获奖信息
   * 
   */

  sendLuck() {
    let _this = this
    let userInfo = {
      userId: app.globalDataO.openid.objectSid,
    }
    app.post(app.url.Lottery, userInfo).then(re => {
      console.log('这是奖项',re)
      var id=re.data.id
      _this.getLuckSum()
      //获奖提示
      _this.setData({
        showPopup:true,
        deskname:re.data.resName,
        textCard: re.data.name,
        showCancel: false, //去掉取消按钮
        isRunning: false
      })
    //   _this.sendvoucher(id)
      // wx.showModal({
      //   title: '恭喜您',
      //   content: '获得了' + _this.data.sale[index].deskName + _this.data.sale[index].text + "优惠券一张",
      //   showCancel: false, //去掉取消按钮
      //   success: function(res) {
      //     if (res.confirm) {
      //       _this.setData({
      //         isRunning: false
      //       })
      //     }
      //   }
      // })
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {
    let userid=app.globalDataO.openid.objectSid
    console.log(userid)
    console.log(userid)
    if (ops.from === 'button') {
      // 来自页面内转发按钮
      console.log(ops.target)
    }
    return {
      title: '快来点餐吧！！',
      path: '/pages/welcome/welcome?userid=' + userid,
      success: function (res) {
        console.log(res);
        console.log("转发成功:" + JSON.stringify(res));
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败:" + JSON.stringify(res));
      }
    }
  },
  //开始游戏
  startGame: function() {
    console.log(this.data.sale)
    if (this.data.degree <= 0 || this.data.degree == null) {
      this._topCue('你已经没有抽奖次数')
      this.setData({
        isRunning: true
      })
      return
    }
    if (this.data.isRunning) return
    this.setData({
      isRunning: true
    })
    var _this = this;
    var indexSelect = this.data.indexSelect
    var i = 0;
    var speed = Math.floor(Math.random() * 30 + 30)
    console.log(speed)
    var timer = setInterval(function() {
      indexSelect++;
      //这里我只是简单粗暴用y=30*x+200函数做的处理.可根据自己的需求改变转盘速度
      i += speed;
      if (i > 1000) {
        //去除循环
        clearInterval(timer)
        i=500
        // 将获奖发送给后台
        _this.sendLuck()
      }
      indexSelect = indexSelect % 8;
      _this.setData({
        indexSelect: indexSelect
      })
    }, (200 + i))
  },
  /**
   * 顶部提示
   */
  _topCue: function(text) {
    this.setData({
      content: text,
      $zanui: {
        toptips: {
          show: true
        }
      }
    });
    setTimeout(() => {
      this.setData({
        $zanui: {
          toptips: {
            show: false
          }
        }
      });
    }, this.data.duration);
  },
})